package cn.hanna.springcloudalibaba.service.impl;

import cn.hanna.springcloudalibaba.dao.OrderMapper;
import cn.hanna.springcloudalibaba.domain.Order;
import cn.hanna.springcloudalibaba.service.AccountService;
import cn.hanna.springcloudalibaba.service.OrderService;
import cn.hanna.springcloudalibaba.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author smileha
 * @create 2021-10-22 23:02
 * @description
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;
    @Resource
    private StorageService storageService;
    @Resource
    private AccountService accountService;

    @Override
    @GlobalTransactional(name = "global_transaction",rollbackFor = Exception.class)
    public void create(Order order) {
        log.info("-------------->开始创建订单");
        orderMapper.create(order);
        log.info("-------------->订单创建完成");

        log.info("-------------->订单微服务开始调用库存,减少count");
        System.out.println("---------------------count-"+order.getCount());
        storageService.decrease(order.getProductId(),order.getCount());
        log.info("-------------->订单微服务调用库存完成");

        log.info("-------------->订单微服务开始调用账户,减少money");
        accountService.decrease(order.getUserId(),order.getMoney());
        log.info("-------------->订单微服务调用账户完成");

        log.info("-------------->修改订单状态");
        orderMapper.updateStatus(order.getId(),1);
        log.info("订单状态修改完成");
        log.info("下订单完成啦啦啦啦O(∩_∩)O");
    }

}
